package com.dgut.dao.impl;

import com.dgut.dao.UserDao;
import com.dgut.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author GooRay
 * 创建于 2020/6/21
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<User> findAll() {
        String sql = "select * from user";
        List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return users;
    }

    @Override
    public Integer findTotalCount(Map<String, String[]> condition) {
        String sql = "select count(*) from user,housing where 1 = 1 and user.user_id = housing.user_id";
        StringBuilder stringBuilder = new StringBuilder(sql);
        Set<String> keys = condition.keySet();
        List<Object> params = new ArrayList<>();
        for (String key : keys) {
            if("currentPage".equals(key)||"rows".equals(key)){
                continue;
            }
            String value = condition.get(key)[0];
            if("temp".equals(key)){
                stringBuilder.append(" and lastest_temp > ? ");
                params.add("%"+value+"%");
                continue;
            }
            if("lastest_temp".equals(key)){
                stringBuilder.append(" and lastest_temp = ? ");
                params.add(value);
                continue;
            }
            if(value!=null&&!"".equals(value)){
                stringBuilder.append(" and "+key+" like ? ");
                params.add("%"+value+"%");
            }
        }
        int totalCount = jdbcTemplate.queryForObject(stringBuilder.toString(), Integer.class,params.toArray());
        return totalCount;
    }

    @Override
    public List<User> findByPage(int start, int rows, Map<String, String[]> condition) {
        String sql = "select * from user,housing where 1 =1 and user.user_id = housing.user_id";
        StringBuilder stringBuilder = new StringBuilder(sql);
        Set<String> keys = condition.keySet();
        List<Object> params = new ArrayList<>();
        for (String key : keys) {
            if("currentPage".equals(key)||"rows".equals(key)){
                continue;
            }
            String value = condition.get(key)[0];
            if("temp".equals(key)){
                stringBuilder.append(" and lastest_temp > ? ");
                params.add(value);
                continue;
            }
            if("lastest_temp".equals(key)){
                stringBuilder.append(" and lastest_temp = ? ");
                params.add(value);
                continue;
            }
            if(value!=null&&!"".equals(value)){
                stringBuilder.append(" and "+key+" like ? ");
                params.add("%"+value+"%");
            }
        }
        stringBuilder.append(" limit ?,? ");
        params.add(start);
        params.add(rows);
        List<User> users = jdbcTemplate.query(stringBuilder.toString(),new BeanPropertyRowMapper<User>(User.class),params.toArray());
        return users;
    }

    @Override
    public List<User> findByTemp(Integer temp) {
        String sql = "select * from user where lastest_temp > ?";
        List<User> users = jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class),temp);
        return users;
    }
}
